package com.villaday.idf.activity;


import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.fragment3.CommonAddressFragment;
import com.villaday.idf.fragment3.CommonGuestFragment;
import com.villaday.idf.util.StatusBarUtil;
import com.villaday.idf.util.ToolBarUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class CommonActivity extends BaseActivity {


    @BindView(R.id.toolbar_back)
    ImageButton toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_next)
    TextView toolbarNext;
    @BindView(R.id.Line_view)
    View LineView;
    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.iv_common_left)
    ImageView ivCommonLeft;
    @BindView(R.id.ll_common_left)
    LinearLayout llCommonLeft;
    @BindView(R.id.iv_common_right)
    ImageView ivCommonRight;
    @BindView(R.id.ll_common_right)
    LinearLayout llCommonRight;
    @BindView(R.id.ll_common_top)
    LinearLayout llCommonTop;
    @BindView(R.id.vp_common)
    ViewPager vpCommon;

    @Override
    protected int getViewResId() {
        return R.layout.activity_common;
    }

    @Override
    protected void init() {
        super.init();
        StatusBarUtil.setTranslucentForImageViewInFragment(this, null);
        StatusBarUtil.setLightMode(this);

        ToolBarUtil.initToolBar(toolbarText, "常用信息", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        toolbarText.setBackgroundResource(R.color.colorBackground);
        initVp();
    }

    /**
     * 初始化ViewPager
     */
    private void initVp() {
        vpCommon.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                switch (i) {
                    case 0:
                        CommonGuestFragment commonGuestFragment = CommonGuestFragment.instance();
                        return commonGuestFragment;
                    case 1:
                        CommonAddressFragment instance = CommonAddressFragment.instance();
                        return instance;
                }
                return null;
            }

            @Override
            public int getCount() {
                return 2;
            }
        });

        vpCommon.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                if (i == 0 && v < 0.6) {
                    ivCommonLeft.setVisibility(View.VISIBLE);
                    ivCommonRight.setVisibility(View.INVISIBLE);
                } else{
                    ivCommonLeft.setVisibility(View.INVISIBLE);
                    ivCommonRight.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }


    @OnClick({R.id.ll_common_left, R.id.ll_common_right})
    public void btnOnclick(View view) {
        switch (view.getId()) {
            case R.id.ll_common_left:
                ivCommonLeft.setVisibility(View.VISIBLE);
                ivCommonRight.setVisibility(View.INVISIBLE);
                vpCommon.setCurrentItem(0);
                break;
            case R.id.ll_common_right:
                ivCommonLeft.setVisibility(View.INVISIBLE);
                ivCommonRight.setVisibility(View.VISIBLE);
                vpCommon.setCurrentItem(1);
                break;
        }
    }

}
